var salt = sessionStorage.getItem('usersalt');
var maxEstimateNum = 0;
var optionType = 0;


layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		laytpl = layui.laytpl,
		table = layui.table;

	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);
	}

	laydate.render({
		elem: '#dexisionTime',
		trigger: 'click'
	});

});

var checkId = getQueryVariable("id");
var editCompanyId;


var company;

if(checkId != false) {
	$('#id').val(checkId);
	$.ajax({
		url: url + 'risk/getInfo',
		dataType: 'json',
		async: false,
		type: "POST",
		data: {
			Token: salt,
			id: checkId
		},
		success: function(data) {
			if(data.code == 0) {
				initEditItem(data.data);
			} else {
				setTimeout(function() {
					layer.msg(data.msg);
				}, 1000);
				checkId = false;
				initItemShow();
			}
		},
		error: function(error) {
			console.log("获取失败");
		}
	});
} else {
}

function addSecurityCheck() {

	if(unit == "") {
		layer.msg('请填单元');
		return;
	}
	var applyUnitId = getTreeVal().toString(); //适用单位id
	if(applyUnitId == 0 || applyUnitId == "") {
		if(checkId != false) { //编辑
			applyUnitId = editCompanyId;
		} else {
			layer.msg('请选择适用单位');
			return;
		}
	}
	var unit = $.trim($("#unit").val()); //单元
	var inventory = $("#inventory").val(); //清单
	var dexisionTime = $("#dexisionTime").val(); //选择时间
	var numberNo = $("#numberNo").val(); //编号
	var dexisionTime = $("#dexisionTime").val(); //选择时间

	//	var itemBool = judgeItemArr();
	//	if(!itemBool) {
	//		layer.msg('请填写完整检查项目');
	//		return;
	//	}
	//	console.log(itemBool)
	//	if(checkId == false) { //新增时
	//		checkId = "";
	//	}
	var postUrl;
	if($('#id').val() == '') {
		postUrl = url + 'risk/add'
	} else {
		postUrl = url + 'risk/edit'
	}
	
	$.ajax({
		url: postUrl,
		dataType: 'json',
		async: false,
		type: "POST",
		data: {
			id: $('#id').val(),
			Token: salt,
			checkId: checkId,
			unit: unit,
			entId: applyUnitId,
			inventory: inventory,
			dexisionTime: dexisionTime,
			share: maxEstimateNum,
			number: numberNo,
			info: JSON.stringify(itemArr)
		},
		success: function(data) {
			if(data.code == 0) {
				var str = "新增提交成功！";
				if(checkId != "") {
					str = "修改提交成功！";
				}
				layer.confirm(str, {
					btn: ['确定'] //按钮
				}, function() {
					location.href = "../list/list.html";
				}, function() {});
			} else {
				layer.msg(data.msg);
			}
		},
		error: function(error) {

			console.log("获取失败");
		}
	});
}


$("body").on("click", ".model .button button", function() {
	$(".model").hide()
});

var letterArr = [];
for (var i = 65, j = 0; i < 91; i++, j++) {
  letterArr[j] = String.fromCharCode(i);
}

//选项排序
sortOption()
function sortOption() {
	$(".option label").each(function (i) {
		$(this).text(letterArr[i]);
  	})
    if(optionType == 2){
        $(".type_option input").hide().siblings().show()
    }else{
        $(".type_option input").show().siblings().hide()
    }
    layui.use(['form'], function() {
        var form = layui.form;
        form.render('checkbox')

    });
}

//添加选项
$("body").on("click",".add_option .box",function () {
	var str = '<div class="layui-input-block option">'+
        '<label></label><input type="checkbox" name="like1[write]" lay-skin="primary">'+
        '<span class="type_option">'+
        '<input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题"'+
    	'class="layui-input option_input"><span>错误</span></span>'+
        '<i class="seraph icon-guanbi"></i>'+
        '<span class="true">【正解】</span><input type="file"></div>';
	$(".add_option").before(str);
  sortOption();
})

//提交
$("body").on("click",".commit",function () {
	var differServ = $("#differServ").val();
    var typeName = $("#typeName").val();
    var questionType = $("#questionType").val();
    var title = $("#title").val();
    var fileId = $("#fileId").val();
    var parse = $("#parse").val();
    var parseFileId = $("#parseFileId").val();
})
